package com.bupt.liutong.admin.core.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.core.bo.UserBo;
import com.bupt.liutong.admin.core.form.UserForm;
import com.bupt.liutong.util.BeanUtils;

public class UserAction extends BaseAction {

	private UserBo userBo;

	/**
	 * 获取用户列表（分页）
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getUsersByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		request.setAttribute("userFormList", userBo.getUsersByPage(userForm, request));
		request.setAttribute("sexMap", ctm.get("sex"));
		request.setAttribute("enableMap", ctm.get("ifEnable"));
		putPageInfo(request, userForm);
		return mapping.findForward("getUsersByPageSuccess");
	}

	/**
	 * 显示添加用户页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward insertUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		//BeanUtils.copyProperties(userForm, new UserForm());
		userForm.setSex(2);
		request.setAttribute("sexList", getOptionList(ctm.get("sex"), false));
		return mapping.findForward("insertUserSuccess");
	}

	/**
	 * 保存添加的用户
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward insertUserDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		userBo.insertUser((UserForm) form);
		BeanUtils.copyProperties(form, new UserForm());
		return mapping.findForward("insertUserDoSuccess");
	}

	/**
	 * 显示修改用户的页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		BeanUtils.copyProperties(form, userBo.getUserByName(userForm));
		request.setAttribute("sexList", getOptionList(ctm.get("sex"), false));
		return mapping.findForward("updateUserSuccess");
	}

	/**
	 * 保存修改的用户
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateUserDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		userBo.updateUser((UserForm) form);
		return mapping.findForward("updateUserDoSuccess");
	}
	public ActionForward changePassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionErrors errors = new ActionErrors();
		UserForm userForm = (UserForm) form;
		UserForm sessionUserForm = (UserForm) request.getSession().getAttribute("userForm");
		userForm.setUserName(sessionUserForm.getUserName());
		if(userBo.changePassword(userForm)){
			BeanUtils.copyProperties(userForm, new UserForm());
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					"error.common", "密码修改成功"));
			saveMessages(request, errors);
			return mapping.findForward("changePassword");
		} else {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					"error.common", "原密码输入错误"));
			saveMessages(request, errors);
			return mapping.findForward("changePassword");
		}
	}

	public ActionForward updateUserRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		request.setAttribute("userFormList", userBo.getUserRolesByUserName(userForm.getUserName()));
		return mapping.findForward("updateUserRoleSuccess");
	}

	public ActionForward updateUserRoleDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		String[] roleIds = request.getParameterValues("roleId");
		userBo.updateUserRole(userForm.getUserName(), roleIds);
		return mapping.findForward("updateUserRoleDoSuccess");
	}
	
	public ActionForward enableUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] userNames = request.getParameterValues("userName");
		userBo.enableUsers(userNames);
		return mapping.findForward("enableUsersSuccess");
	}
	
	public ActionForward disableUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] userNames = request.getParameterValues("userName");
		userBo.disableUsers(userNames);
		return mapping.findForward("disableUsersSuccess");
	}

	/**
	 * 删除一个或多个用户
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward deleteUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] userNames = request.getParameterValues("userName");
		userBo.deleteUsers(userNames);
		return mapping.findForward("deleteUsersSuccess");
	}

	/**
	 * 为Spring注入Bo而写
	 * 
	 * @param userBo
	 */
	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}
}
